package com.van.entity;

import com.van.entity.constant.ResStatus;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * 统一回复样式
 * @author VanKa qq:2395073039
 * @date 2021/8/16 11:32
 */
@Data
@ApiModel(description= "统一回复样式")
public class ResPonObject implements Serializable {
    @ApiModelProperty(value = "数据")
    private Object data;
    @ApiModelProperty(value = "查询数据总量")
    private int count;
    @ApiModelProperty(value = "回复结果")
    private String msg = "";
    @ApiModelProperty(value = "响应错误码 暂时没用")
    private int code = 0;
    @ApiModelProperty(value = "请求处理状态")
    private ResStatus status;
    public String toJson() {
        return "{ \"msg\":\"" + msg + "\", \"code\":\"" + code + "\", \"status\":\"" + status +  "\"}";
    }

    public ResPonObject() {
        this.status = ResStatus.SUCCESS;
    }

    public ResPonObject(int count, Object data, String msg, ResStatus status) {
        this.data = data;
        this.count = count;
        this.msg = msg;
        this.status = status;
    }
    public ResPonObject(Object data, String msg, ResStatus status) {
        this.data = data;
        this.count = 0;
        this.msg = msg;
        this.status = status;
    }
    public ResPonObject(boolean on, Object data, String msg) {
        this.data = data;
        this.count = 0;
        this.msg = msg;
        this.status = on ? ResStatus.SUCCESS :ResStatus.ERROR;
    }
    public ResPonObject(String msg, ResStatus status) {
        this.count = 0;
        this.msg = msg;
        this.status = status;
    }

    private static ResPonObject success(Object data) {
        return new ResPonObject(0, data, null, ResStatus.SUCCESS);
    }

    private static ResPonObject success(Object data, String msg) {
        return new ResPonObject(0, data, msg, ResStatus.SUCCESS);
    }

    private static ResPonObject success(int count, Object data, String msg) {
        return new ResPonObject(count, data, msg, ResStatus.SUCCESS);
    }

    private static ResPonObject fail(String msg) {
        return new ResPonObject(0, null, msg, ResStatus.ERROR);
    }

    private static ResPonObject fail(Object data, String msg) {
        return new ResPonObject(0, data, msg, ResStatus.ERROR);
    }

    private static ResPonObject fail(int count, Object data, String msg) {
        return new ResPonObject(count, data, msg, ResStatus.ERROR);
    }

    private static ResPonObject revert(String msg, ResStatus status) {
        return new ResPonObject(0, null, msg, status);
    }

    private static ResPonObject revert(Object data, String msg, ResStatus status) {
        return new ResPonObject(0, data, msg, status);
    }

    private static ResPonObject revert(int count, Object data, String msg) {
        if (count <= 0)
            return new ResPonObject(count, data, msg, ResStatus.ERROR);

        return new ResPonObject(count, data, msg, ResStatus.SUCCESS);
    }

    private static ResPonObject revert(boolean on, Object data, String msg) {
        if (on)
            return new ResPonObject(0, data, msg, ResStatus.SUCCESS);

        return new ResPonObject(0, data, msg, ResStatus.ERROR);
    }

}
